# -*- coding: utf-8 -*-
"""
__mktime__ = '2019/4/13'
__author__ = 'Just'
__filename__ = 'redprint'
"""
from flask import Blueprint

class Redprint:
    """拆分视图"""
    def __init__(self, name):
        """
        :param name: 视图文件名
        """
        self.name = name
        self.mound = []

    def route(self, rule, **options):
        """
        注册视图
        :param rule: 路由规则
        :param options: 参数
        :return:
        """
        def decorator(f):
            self.mound.append((f, rule, options))
            return f
        return decorator

    def register(self, bp, url_prefix=None):
        """
        蓝图注册视图
        :param bp: 蓝图对象
        :param url_prefix: 路由前缀
        :return:
        """
        if url_prefix is None:
            url_prefix = '/' + self.name
        for f, rule, options in self.mound:
            # 定制 endpoint 格式
            endpoint = self.name + '+' + options.pop("endpoint", f.__name__)
            bp.add_url_rule(url_prefix + rule, endpoint, f, **options)
